package org.yxing.palpalgps.activity;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.yxing.palpalgps.AbstractActivity;
import org.yxing.palpalgps.R;
import org.yxing.palpalgps.adapter.SelectFriendAdapter;
import org.yxing.palpalgps.http.AsyncRequest;
import org.yxing.palpalgps.http.AsyncRequest.FetchListener;
import org.yxing.palpalgps.models.SimpleUser;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class SelectFriendActivity extends AbstractActivity implements OnItemClickListener, OnClickListener {
    
    private static final String TAG = SelectFriendActivity.class.getSimpleName();
    
    private SelectFriendAdapter mAdapter;
    private ListView mListView;
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.select_friend);
        setTitle(getString(R.string.select_friend));
        
        mListView = (ListView) findViewById(android.R.id.list);
        mListView.setOnItemClickListener(this);
        mAdapter = new SelectFriendAdapter(this);
        mListView.setAdapter(mAdapter);
        mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        
        findViewById(R.id.buttonSave).setOnClickListener(this);
        
        
        fetchData();
    }
    
    private void fetchData() {
        AsyncRequest ar = new AsyncRequest(this);
        ar.fetch("friend/list", new FetchListener() {
            
            @Override
            public void onError(int code, String msg) {
                
            }
            
            @Override
            public void onComplete(JSONObject response) {
                try {
                    JSONObject result = response.getJSONObject("result");
                    JSONArray friend = result.getJSONArray("friends");
                    for (int i = 0; i < friend.length(); i ++) {
                        JSONObject obj = friend.getJSONObject(i);
                        SimpleUser simpleUser = new SimpleUser();
                        simpleUser.setUid(obj.getString("uid"));
                        simpleUser.setName(obj.getString("name"));
                        simpleUser.setPortrait(obj.getString("picture"));
                        mAdapter.add(simpleUser);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        SimpleUser su = mAdapter.getItem(position);
        su.setSelected(!su.isSelected());
        mAdapter.notifyDataSetChanged();
    }
    
    /**
     * get all selected user id
     * @return selected user id
     */
    private ArrayList<String> getSelectedUids() {
        ArrayList<String> uids = new ArrayList<String>(); 
        for (int i = 0; i < mAdapter.getCount(); i ++) {
            SimpleUser user = mAdapter.getItem(i);
            if (user.isSelected())
                uids.add(user.getUid());
        }
        
        return uids;
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.buttonSave) {
            Intent intent = new Intent();
            intent.putStringArrayListExtra("uids", getSelectedUids());
            setResult(RESULT_OK, intent);
            finish();
        }
    }
}
